package com.taomu.app.zongchouhuli.moudle.mine.activity;

import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.framework.BaseActivity;
import com.taomu.app.zongchouhuli.framework.BasePresenter;
import com.taomu.app.zongchouhuli.moudle.mine.contract.ModifyGenderContract;
import com.taomu.app.zongchouhuli.moudle.mine.contract.impl.ModifyGenderPresenter;
import com.taomu.app.zongchouhuli.utils.BaseConst;

public class ModifyGenderActivity extends BaseActivity<ModifyGenderContract.Presenter> implements ModifyGenderContract.View {

    private ImageView mBackIm;

    private RadioGroup mGroupRadio;

    private RadioButton mMaleBtn;

    private RadioButton mFemaleBtn;

    private TextView mSaveTv;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mPresenter != null){
            mPresenter.onDetch();
        }
    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_modify_gender;
    }

    @Override
    protected void initView() {
        mBackIm = findViewById(R.id.modify_gender_back_im);
        mGroupRadio = findViewById(R.id.sex_rg);
        mMaleBtn = findViewById(R.id.male_rb);
        mFemaleBtn = findViewById(R.id.famale_rb);
        mSaveTv = findViewById(R.id.modify_gender_save_tv);
    }

    @Override
    protected void initData() {
        if(getIntent() != null){
            String gender = getIntent().getStringExtra(BaseConst.MODIFYGENDER);
            if("男".equals(gender)){
                mMaleBtn.setChecked(true);
            }else{
                mFemaleBtn.setChecked(true);
            }
        }
    }

    @Override
    protected void initEvent() {
        mBackIm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                back();
            }
        });
        mSaveTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mFemaleBtn.isChecked()) {
                    mPresenter.getGender("1");
                }else if(mMaleBtn.isChecked()){
                    mPresenter.getGender("0");
                }
            }
        });
    }

    @Override
    protected ModifyGenderContract.Presenter createPresenter() {
        return new ModifyGenderPresenter(this);
    }

    @Override
    public void setGender(String msg) {
        showToast("修改成功");
    }

    @Override
    public void setPresenter(ModifyGenderContract.Presenter presenter) {

    }

    private void back(){
        Intent intent = new Intent();
        if(mFemaleBtn.isChecked()) {
            intent.putExtra(BaseConst.MODIFYGENDER, "female");
        }else if(mMaleBtn.isChecked()){
            intent.putExtra(BaseConst.MODIFYGENDER, "male");
        }
        setResult(0x22,intent);
        finish();

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            back();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
